package org.wsy;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/reservations")
public class ReservationApiGatewayRestController {

	@Autowired
    private DiscoveryClient discoveryClient;

//  @LoadBalanced
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/names")
    public Collection<String> getReservationNames(){
        ParameterizedTypeReference<Resources<Reservation>> ptr = 
                new ParameterizedTypeReference<Resources<Reservation>>() { };
        String url = discoveryClient.getInstances("reservation-service").get(0).getUri()+"/reservations";
        ResponseEntity<Resources<Reservation>> responseEntity = 
                this.restTemplate.exchange(url, 
                HttpMethod.GET, null, ptr);
        return responseEntity
                .getBody()
                .getContent()
                .stream()
                .map(Reservation::getReservationName)
                .collect(Collectors.toList());
    }
}
